# coding:utf-8
from django.utils import timezone
from datetime import datetime


def get_current_time_by_microsecond():
    """
    将当前时间计算成微秒
    :return: 微妙
    """
    return int(timezone.now().timestamp() * 1000000)


def to_local_time(d):
    if isinstance(d, datetime):
        return d.replace(tzinfo=timezone.utc)
    else:
        raise TypeError


def time_to_string(d):
    if isinstance(d, datetime):
        return d.strftime("%Y-%m-%d %H:%M:%S")
    else:
        raise TypeError


def string_to_time(s):
    if isinstance(s, str):
        return datetime.strptime(s, "%Y-%m-%d %H:%M:%S")
    else:
        raise TypeError


def time_to_timestamp(d):
    if isinstance(d, str):
        return string_to_time(d).timestamp()
    elif isinstance(d, datetime):
        return d.timestamp()
    else:
        raise TypeError
